package com.maxatime.service.impl;

import com.maxatime.dao.EmployeeInfoMapper;
import com.maxatime.dao.EmployeeRegisterMapper;
import com.maxatime.dao.StudentMessageMapper;
import com.maxatime.dao.StudentRegisterMapper;
import com.maxatime.pojo.EmployeeInfo;
import com.maxatime.pojo.EmployeeRegister;
import com.maxatime.pojo.StudentMessage;
import com.maxatime.pojo.StudentRegister;
import com.maxatime.service.RegisterService;
import com.maxatime.utils.PasswordManagerUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;


@Service
public class RegisterServiceImpl implements RegisterService {
    Logger log = LogManager.getLogger();
    @Autowired
    private EmployeeInfoMapper employeeInfoMapper;
    @Autowired
    private EmployeeRegisterMapper employeeRegisterMapper;
    @Autowired
    private StudentRegisterMapper studentRegisterMapper;
    @Autowired
    private StudentMessageMapper studentMessageMapper;


    @Override
    public Map<String, Object> saveRegister(String employeeName, String email, String password) {


        Map<String, Object> m=new HashMap<>();


//      String employeeId=  UUID.randomUUID().toString().replace("-","");
        String employeeId=System.currentTimeMillis()+"";
        EmployeeInfo employeeInfo = new EmployeeInfo();
        employeeInfo.setEmployeeName(employeeName);
        employeeInfo.setEmail(email);
        employeeInfo.setEmployeeId(employeeId);
        int info = employeeInfoMapper.insertSelective(employeeInfo);
        log.info("employeeinfo "+employeeInfo);



        EmployeeRegister employeeRegister = new EmployeeRegister();
        employeeRegister.setEmail(email);
        String md5DigestAsHex = PasswordManagerUtil.getMd5DigestAsHex(password);
        employeeRegister.setPassword(md5DigestAsHex);
        employeeRegister.setEmployeeId(employeeId);
        int iReg= employeeRegisterMapper.insertSelective(employeeRegister);
        
         m.put("code","0000");
         m.put("msg","注册成功");



        return m;
    }

    @Override
    public EmployeeRegister login(String email, String password) {
        EmployeeRegister employeeRegister = new EmployeeRegister();
        String md5DigestAsHex = PasswordManagerUtil.getMd5DigestAsHex(password);
        employeeRegister.setPassword(md5DigestAsHex);
        return employeeRegisterMapper.login(email,md5DigestAsHex);
    }

    @Override
    public Map<String, Object> sRegister(String email, String password) {
        Map<String, Object> m=new HashMap<>();


//      String student_id=  UUID.randomUUID().toString().replace("-","");
//        String student_id=System.currentTimeMillis()+"";
        StudentRegister studentRegister = new StudentRegister();
        studentRegister.setStudentId("1");
        studentRegister.setEmail(email);
        String md5DigestAsHex = PasswordManagerUtil.getMd5DigestAsHex(password);
        studentRegister.setPassword(md5DigestAsHex);
        studentRegister.setStatus("0");
        studentRegister.setDeleteFlag("0");

        Calendar now = Calendar.getInstance();
        studentRegister.setCreateTime(now.getTime());
        studentRegister.setUpdateTime(now.getTime());
        int i = studentRegisterMapper.insertSelective(studentRegister);
        log.info("studentRegisterMapper.insertSelective:::"+i);
        m.put("code","0000");
        m.put("msg","注册成功");



        return m;
    }

    @Override
    public StudentRegister loginn(String email, String password) {
        StudentRegister studentRegister = new StudentRegister();
        String md5DigestAsHex = PasswordManagerUtil.getMd5DigestAsHex(password);
        studentRegister.setPassword(md5DigestAsHex);
        return studentRegisterMapper.loginn(email,md5DigestAsHex);

    }


    @Override
    public StudentMessage selectKey(String studentId) {
        return studentMessageMapper.selectKey(studentId);
    }

}
